package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.MallBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_point_mall_aty.*
import java.util.ArrayList

class PointMallAty : BaseAty() {
    private val datas= ArrayList<MallBean.DataBeanX>()
    private lateinit var adp: BaseMultiItemQuickAdapter<MallBean.DataBeanX, BaseViewHolder>
    override fun getContentId(): Int {
        return R.layout.activity_point_mall_aty
    }

    override fun initView() {

        adp=object :BaseMultiItemQuickAdapter<MallBean.DataBeanX,BaseViewHolder>(datas){
            init {
                addItemType(1, R.layout.read_book)
                addItemType(2, R.layout.read_book)
            }
            override fun convert(helper: BaseViewHolder?, item: MallBean.DataBeanX?) {
                when(helper?.itemViewType){
                    1->{
                        helper?.setText(R.id.tvTittle, item?.name)
                        val recyclerView = helper?.getView<RecyclerView>(R.id.recyclerview)
                        if (item?.data!=null){
                            val adp= object :BaseQuickAdapter<MallBean.DataBeanX.DataBean,BaseViewHolder>(R.layout.potinmall_book_item,item?.data){
                                override fun convert(helper: BaseViewHolder?, item: MallBean.DataBeanX.DataBean?) {
                                    Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
                                    helper?.setText(R.id.itemTittle,item?.title)
                                    helper?.setText(R.id.itemPoint,item?.score.toString())

                                }
                            }
                            recyclerView.adapter=adp
                            recyclerView.layoutManager=LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false)
                            adp.setOnItemClickListener { adapter, view, position ->
                                val intent = Intent(mContext, HtmlAty::class.java)
                                intent.putExtra("id",item?.data[position].id)
                                intent.putExtra("et","1")
                                mContext.startActivity(intent)
                            }
                        }
                    }
                    2->{
                        helper?.setText(R.id.tvTittle,item?.name)
                        val recyclerView = helper?.getView<RecyclerView>(R.id.recyclerview)
                        if (item?.data!=null){
                            val adp= object :BaseQuickAdapter<MallBean.DataBeanX.DataBean,BaseViewHolder>(R.layout.potinmall_class_item,item?.data){
                                override fun convert(helper: BaseViewHolder?, item: MallBean.DataBeanX.DataBean?) {
                                    Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
                                    helper?.setText(R.id.itemTittle,item?.title)
                                    helper?.setText(R.id.itemPoint,item?.score.toString())

                                }
                            }
                            recyclerView.adapter=adp
                            recyclerView.layoutManager=GridLayoutManager(mContext,2)
                            adp.setOnItemClickListener { adapter, view, position ->
                                val intent = Intent(mContext, HtmlAty::class.java)
                                intent.putExtra("id",item?.data[position].id)
                                intent.putExtra("et","1")
                                startActivity(intent)
                            }
                        }
                    }
                }
            }
        }
        recyclerview.adapter=adp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayout.VERTICAL,false)
        getData()
    }

    private fun getData() {
        OkGo.post<MallBean>(Urls.BaseUrl + Urls.POINT_MALL)
                .params("type", 1)
                .execute(object : JsonCallBack<MallBean>(this) {
                    override fun onSuccess(response: Response<MallBean>?) {
                        val bean = response?.body()
                        if (bean?.code == 1) {
                            for (i in 0..bean?.data.size-1){
                                if (i==0){
                                    bean?.data[i].setType(1)
                                }else{
                                    bean?.data[i].setType(2)
                                }
                            }
                            datas.addAll(bean?.data)
                            adp.notifyDataSetChanged()
                        }
                    }
                })
    }

    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }
    }

}
